package com.algoshow.controller;

import com.algoshow.model.ListNode;
import com.algoshow.view.SecondMenu;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author vvv
 * @date 2023-05-21 16 25
 * discription
 */
public class ListNodePage extends BasePage {

    private ListNode head;

    private Integer index;

    private Integer value;

    private ListNode insertingNode;
    // 监控变量名
    private ListNode cur;

    private final int NODE_SIZE = 50 + 20;

    public ListNodePage(ListNode head, int index, int value, String algoName) {
        this.head = head;
        this.index = index;
        this.value = value;
        super.algoName = algoName;

        firstMenuButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (timer != null && timer.isAlive()) {
                    timer.stop();
                }
                dispose();
                new SecondMenu().showTest5();
            }
        });
    }

    @Override
    public void drawAlgo(Graphics g) {
        drawLinkedList(g);
    }

    @Override
    public void drawParam(Graphics g) {
        if (cur == null) {
            cur = new ListNode(-1);
        }
        // 坐标
        int x = paramX, y = paramY;
        g.drawString("cur.value = ", x, y);
        g.drawString(cur.value.toString(), x + 180, y);
    }

    private void drawLinkedList(Graphics g) {
        ListNode currentNode = head;
        int x = startX;
        int y = startY;

        while (currentNode != null) {
            g.setColor(currentNode.color);
            g.fillOval(x, y - NODE_SIZE / 2, NODE_SIZE, NODE_SIZE);
            g.setColor(Color.BLACK);
            g.drawString(String.valueOf(currentNode.value), x + NODE_SIZE / 2 - 5, y + 5);

            if (currentNode.next != null) {
                g.setColor(Color.BLACK);
                g.drawLine(x + NODE_SIZE, y, x + NODE_SIZE + 50, y);
                g.drawLine(x + NODE_SIZE + 50, y, x + NODE_SIZE + 45, y - 5);
                g.drawLine(x + NODE_SIZE + 50, y, x + NODE_SIZE + 45, y + 5);
            }

//            if (insertingNode != null && currentNode.next == insertingNode) {
//                // 画节点
//                g.setColor(insertingNode.color);
//                g.fillOval(x + 25, y - NODE_SIZE / 2 + 50, NODE_SIZE, NODE_SIZE);
//                g.setColor(Color.BLACK);
//                g.drawString(String.valueOf(insertingNode.value), x + NODE_SIZE / 2 - 5 + 25, y + 5 + 50);
//                //画箭头
//
//                insertingNode = null;
//            }
            x += NODE_SIZE + 50;
            currentNode = currentNode.next;
        }
    }

    @Override
    public void startThread() {
        timer = new Thread() {
            @Override
            public void run() {
                switch (algoName) {
                    case "listInsert":
                        insert(index, value);
                        break;
                    case "listDelete":
                        delete(index);
                        debug();
                        break;
                    default:
                        timer.stop();
                }
            }
        };
        timer.start();
    }
    // 链表i位插入
    private void insert(int index, int value) {
        index--;
        render(0);
        cur = head;
        cur.color = GREEN;
        render(1);
        cur.color = blur;
        while (index > 0) {
            render(2);
            cur = cur.next;
            cur.color = GREEN;
            render(3);
            cur.color = blur;
            index--;
            render(4);
        }

        ListNode node = new ListNode(value);
        // 重点是生成这段指正变换的逻辑
        node.color = red;
        insertingNode = node;
        render(6);
        node.next = cur.next;
        render(7);
        cur.next = node;
        render(8);
        render(0);
    }

    // TODO 链表i位删除
    private void delete(int index) {
        index--;
        render(0);
        cur = head;
        cur.color = GREEN;
        render(1);
        cur.color = blur;
        while (index > 0) {
            render(2);
            cur = cur.next;
            cur.color = GREEN;
            render(3);
            cur.color = blur;
            index--;
            render(4);
        }
        render(6);
        cur.next.color=red;
        render(null, 300);
        cur.next = cur.next.next;
        render();
    }

    public void debug() {
        ListNode cur = head;
        while (cur != null) {
            System.out.println(cur);
            cur = cur.next;
        }
    }
}
